package com.smmisia.article.controller;

import com.smmisia.article.service.impl.FileService;
import com.smmisia.common.Result.Result;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * 文件上传
 * @author smmisia
 * @date 2023/7/27
 */
@RestController
public class FileController {

    @Resource
    private FileService fileService;


    @RequestMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }

    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传图片(feign)
     */
    @PostMapping(value = "/uploadFeign",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public HashMap<String, String> uploadFeign(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return fileService.upload(uploadFile);
    }

    /**
     * 二维码生成(feign)
     */
    @PostMapping(value = "/qrcode",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public HashMap<String,String> qrcode(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return fileService.qrcode(uploadFile);
    }

    /**
     * 上传头像
     */
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return fileService.uploadFile(uploadFile);
    }

    /**
     * 上传图片
     */
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result upload(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return fileService.uploadImage(uploadFile);
    }

    /**
     * 批量上传
     */
    @PostMapping(value = "/batchUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result batchUpload(@RequestParam("file") MultipartFile[] uploadFile) throws Exception {
        return fileService.batchUpload(uploadFile);
    }

    @PostMapping(value = "/uploadArticle",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result uploadArticle(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return fileService.uploadArticle(uploadFile);
    }

    @PostMapping(value = "/uploadBatchArticle",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public List<String> uploadBatchArticle(@RequestParam("file") MultipartFile[] uploadFile) throws Exception {
        return fileService.uploadBatchArticle(uploadFile);
    }

}
